package samples.java.algorithm.sort;

/**
 * @author wang.yuchuan
 * @createdAt 2018/12/9
 */
public class CountingSort {

    public static void sort(int[] arr, int maxValue) {
        int[] bucket = new int[maxValue + 1];

        int sortedIndex = 0;

        for (int i = 0; i < arr.length; i++) {
            int value = arr[i];
            bucket[value]++;
        }

        for (int j = 0; j < bucket.length; j++) {
            while (bucket[j] > 0) {
                arr[sortedIndex++] = j;
                bucket[j]--;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {3, 44, 5, 67, 4, 23, 42, 56, 78, 91, 2, 7, 9, 12, 16};

        sort(arr, 100);

        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
}
